﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_DesignListFileBusiness : BaseBusiness<PB_DesignListFile>, IPB_DesignListFileBusiness, ITransientDependency
    {
        public PB_DesignListFileBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_DesignListFile>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_DesignListFile>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_DesignListFile, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_DesignListFile> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_DesignListFile data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_DesignListFile data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task AddDesignListFile(PB_DesignListDetailsDing data)
        {
            PB_DesignListFile pB_DesignListFile = new PB_DesignListFile();
            //基础
            if (data.Type == 0)
            {

                pB_DesignListFile.Id = IdHelper.GetId();
                pB_DesignListFile.CreatorId = "";
                pB_DesignListFile.CreateTime = DateTime.Now;
                pB_DesignListFile.DetailedFile = data.DetailedFile;
                pB_DesignListFile.DesignId = data.DesignID;
                pB_DesignListFile.VersionNumber = 0;
                pB_DesignListFile.Type = data.Type;
                pB_DesignListFile.Deleted = false;
                string sqldelete = "delete from  PB_DesignListFile where   DesignId='" + data.DesignID + "' And  Type=0";
                await Db.ExecuteSqlAsync(sqldelete);
            } else if (data.Type==2) {
                string sql = "select IFNULL(max(VersionNumber),0)+1  VersionNumber from PB_DesignListFile  WHERE  DesignId='" + data.DesignID + "' and  Type=2";
                var db = await Db.GetListBySqlAsync<PB_DesignListFile>(sql.ToString());
                pB_DesignListFile.Id = IdHelper.GetId();
                pB_DesignListFile.CreatorId = "";
                pB_DesignListFile.CreateTime = DateTime.Now;
                pB_DesignListFile.DetailedFile = data.DetailedFile;
                pB_DesignListFile.DesignId = data.DesignID;
                pB_DesignListFile.VersionNumber = db[0].VersionNumber;
                pB_DesignListFile.Type = data.Type;
                pB_DesignListFile.Deleted = false;
                string sqldelete = "delete from  PB_DesignListFile where   DesignId='" + data.DesignID + "' And  Type=2";
                await Db.ExecuteSqlAsync(sqldelete);

            }
            else {
                //查出最大版本号
                string sql = "select IFNULL(max(VersionNumber),0)+1  VersionNumber from PB_DesignListFile  WHERE  DesignId='" + data.DesignID + "' and  Type=1";
                var db = await Db.GetListBySqlAsync<PB_DesignListFile>(sql.ToString());
                pB_DesignListFile.Id = IdHelper.GetId();
                pB_DesignListFile.CreatorId = "";
                pB_DesignListFile.CreateTime = DateTime.Now;
                pB_DesignListFile.DetailedFile = data.DetailedFile;
                pB_DesignListFile.DesignId = data.DesignID;
                pB_DesignListFile.VersionNumber = db[0].VersionNumber;
                pB_DesignListFile.Type = data.Type;
                pB_DesignListFile.Deleted = false;
                string sqldelete = "delete from  PB_DesignListFile where   DesignId='" + data.DesignID + "' And  Type=1";
                await Db.ExecuteSqlAsync(sqldelete);
            }
            await Db.InsertAsync(pB_DesignListFile);
            
        }

        #endregion

        #region 私有成员

        #endregion
    }
}